<?php

namespace app\admin\product\model;

use mb\helper\Collection;
use think\Db;

class Message
{
    protected static $table = 'product_messages';

    /**
     * @param array $row
     * @return bool|int|string
     */
    public static function add(array $row)
    {
        $newRow = Collection::elements([
            'productId',
            'expect',
            'nickname',
            'value',
            'money',
            'moneyOrder',
            'memberId',
        ], $row);
        $newRow['timeCreated'] = time();
        $newRow = Collection::keyStyle($newRow, Collection::NAME_STYLE_C);
        try {
            return Db::table(self::$table)
                ->insertGetId($newRow);
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @param $filter
     * @return array|Error
     */
    public static function fetch($filter,$connect = null)
    {
        $where = self::parseFilters($filter);
        try {
            if (empty($connect)) {
                $connect = Db::connect();
            }
            $row = $connect->table(self::$table)
                ->where($where)
                ->order('expect','desc')
                ->find();
            if (!empty($row)) {
                $row['time_created'] *= 1000;
                $row['time_open'] *= 1000;
                return Collection::keyStyle($row, Collection::NAME_STYLE_JAVA);
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return [];
    }

    /**
     * @param $filter
     * @return bool|Error
     */
    public static function remove($filter)
    {
        $where = self::parseFilters($filter);
        try {
            $offect = Db::table(self::$table)
                ->where($where)
                ->delete();
            if ($offect === 1) {
                return true;
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return $offect;
    }

    /**
     * @param $filter
     * @param $newState
     * @return bool|Error
     */
    public static function update($filter, $newState)
    {
        $where = self::parseFilters($filter);
        try {
            $offect = Db::table(self::$table)
                ->where($where)
                ->update($newState);
            if ($offect === 1) {
                return true;
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return $offect;
    }

    /**
     * @param $filter
     * @return array
     */
    public static function parseFilters($filter)
    {
        $where = [];
        if (is_array($filter)) {
            if (!empty($filter['id'])) {
                $where[] = ['id', '=', $filter['id']];
            }
            if (!empty($filter['name']) && !empty($filter['expect'])) {
                $where[] = ['name', '=', $filter['name']];
                $where[] = ['expect', '=', $filter['expect']];
            }
            if (!empty($filter['timeCreatedEnd'])) {
                $where[] = ['time_created', '<=', $filter['timeCreatedEnd']];
            }
            if (empty($where)) {
                throw error(-10, '参数错误必须指定id');
            }
        } else {
            $where[] = ['id', '=', intval($filter)];
        }
        return $where;
    }

    /**
     * @param array $filters
     * @param int $pIndex
     * @param int $pSize
     * @param int $total
     * @return array|Error
     */
    public static function search(array $filters, int $pIndex = 0, int $pSize = 10, int &$total = 0,$connect = null)
    {
        $where = [];
        if (!empty($filters['productId'])) {
            $where[] = ['product_id', '=', $filters['productId']];
        }
        if (!empty($filters['expect'])) {
            $where[] = ['expect', '=', $filters['expect']];
        }
        if (!empty($filters['timeCreatedStart'])) {
            $where[] = ['time_created', '>=', $filters['timeCreatedStart']];
        }
        if (!empty($filters['timeCreatedEnd'])) {
            $where[] = ['time_created', '<=', $filters['timeCreatedEnd']];
        }
        if (!empty($filters['idStart'])) {
            $where[] = ['id', '>=', $filters['idStart']];
        }
        try {
            if (empty($connect)) {
                $connect = Db::connect();
            }
            if (!empty($pIndex)) {
                $total = $connect->table(self::$table)
                    ->where($where)
                    ->count();
            }
            $query = $connect->newQuery()->table(self::$table)
                ->where($where);
            if (!empty($pIndex)) {
                $query->page($pIndex, $pSize);
            }
            if (!empty($filters['order'])) {
                $query->order($filters['order']);
            }
            $dataSet = $query->select();
            if (!$dataSet->isEmpty()) {
                return $dataSet->map(
                    function ($row) {
                        $row['time_created'] *= 1000;
                        $row = Collection::keyStyle($row, Collection::NAME_STYLE_JAVA);
                        return $row;
                    }
                )->toArray();
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return [];
    }
}